<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="org.ilead.iserve.service.manager.MessageManager"%>
<%@page import="java.util.Date"%>
<%@page import="org.ilead.iserve.model.PrivateMessage"%>
<%@page import="org.ilead.iserve.model.User"%>
<%@page import="org.ilead.iserve.constants.SessionKeyConstant"%>
<%@page import="java.util.Map"%>
<%@page import="org.ilead.iserve.service.manager.UserManager"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	boolean isInvalid = false;
	String action = request.getParameter("action");
	String title = request.getParameter("title");
	if(title != null && !"".equals(title))
		title = URLDecoder.decode(title, "UTF-8");
	String content = request.getParameter("content");
	if(content != null && !"".equals(content)){
		content = URLDecoder.decode(content, "UTF-8");
	}
	String target = request.getParameter("target");
	String source = request.getParameter("source");
	String message = request.getParameter("messageId");
	if("send".equalsIgnoreCase(action)){
		try{
			int sourceId = ((User)session.getAttribute(SessionKeyConstant.LOGIN_USER)).getId();
			int targetId = Integer.parseInt(target);
			User user = UserManager.getInstance().findById(targetId);
			PrivateMessage m = new PrivateMessage();
			m.setTitle(title);
			m.setSource(sourceId);
			m.setContent(content);
			m.setTarget(targetId);
			m.setSendTime(new Date());
			m.setReState(0);
			m.setSeState(0);
			MessageManager.getInstance().addPrivateMessage(m);
			isInvalid = false;
		}catch(Exception e){
			isInvalid = true;
		}
	}else if("delete".equalsIgnoreCase(action)){
		int messageId = Integer.parseInt(message);
		try{
			PrivateMessage m = MessageManager.getInstance().getPrivateMessageById(messageId);
			m.setReState(-1);
			MessageManager.getInstance().updatePrivateMessage(m);
			isInvalid = false;
		}catch(Exception e){
			isInvalid = true;
		}
	}else if("reply".equalsIgnoreCase(action)){
		try{
			int sourceId = ((User)session.getAttribute(SessionKeyConstant.LOGIN_USER)).getId();
			int messageId = Integer.parseInt(message);
			int targetId = Integer.parseInt(source);
			PrivateMessage sourceMsg = MessageManager.getInstance().getPrivateMessageById(messageId);
			PrivateMessage m = new PrivateMessage();
			m.setTitle("re: " + sourceMsg.getTitle());
			m.setSource(sourceId);
			m.setContent(content);
			m.setTarget(targetId);
			m.setSendTime(new Date());
			m.setReState(0);
			m.setSeState(0);
			
			MessageManager.getInstance().addPrivateMessage(m);
			isInvalid = false;
		}catch(Exception e){
			isInvalid = true;
		}
	}else if("read".equalsIgnoreCase(action)){
		try{
			int messageId = Integer.parseInt(message);
			PrivateMessage m = MessageManager.getInstance().getPrivateMessageById(messageId);
			m.setReState(1);
			MessageManager.getInstance().updatePrivateMessage(m);
			isInvalid = false;
		}catch(Exception e){
			isInvalid = true;
		}
	}else if("sendDelete".equalsIgnoreCase(action)){
		try{
			int messageId = Integer.parseInt(message);
			PrivateMessage m = MessageManager.getInstance().getPrivateMessageById(messageId);
			m.setSeState(-1);
			MessageManager.getInstance().updatePrivateMessage(m);
			isInvalid = false;
		}catch(Exception e){
			isInvalid = true;
		}
	}
	//AJAX
	response.setContentType("text/xml");
	response.setHeader("Cache-Control","no-stroe");
	response.setHeader("pragma","no-cache");
	response.setDateHeader("Expries",0);
	String msg = "";
	if(isInvalid == true) {
		msg = "invalid";
	}else{
		msg = "validate";
	}
	response.getWriter().write("<msg>"+ msg +"</msg>");
%>
